package org.palmelf.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.palmelf.model.Phontograph;
import org.palmelf.service.PhontographService;
import org.palmelf.service.PhontographServiceImpl;

/**
 * 为了测试web.xml有没有被扫描到
 * 
 * @author jacarri
 * 
 */
public class WebxmlServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/* 解决中文乱码的问题 */
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json");
		PrintWriter out = resp.getWriter();

		PhontographService phontographService = new PhontographServiceImpl();
		// TODO 刷新列表数据
		out.write(toString1(phontographService.fetchPhontographList()));
	}

	public String toString(List<Phontograph> s) {
		String str = "[";
		for (int i = s.size() - 1; i >= 0; i--) {
			Phontograph ph = s.get(i);
			str += "{'pid':'" + ph.getPid() + "','carNum':'" + ph.getCarNum()
					+ "','pName':'" + ph.getpName() + "','pDate':'"
					+ ph.getpDate() + "','pPhonto':'" + ph.getpPhonto() + "'},";
		}
		str = str.substring(0, str.length()) + "]";
		System.out.println("----------" + str);
		return str;
	}

	public String toString1(List<Phontograph> s) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = "[";
		for (int i = 0; i <= s.size() - 1; i++) {
			Phontograph ph = s.get(i);
			str += "{'pid':'" + ph.getPid() + "','carNum':'" + ph.getCarNum()
					+ "','pName':'" + ph.getpName() + "','pDate':'"
					+ sdf.format(ph.getpDate()) + "','pPhonto':'"
					+ ph.getpPhonto() + "'},";
		}
		str = str.substring(0, str.length()) + "]";
		return str;
	}
}
